<?php

namespace app\service;

use app\model\BookType as BookTypeModel;
use app\model\Member as MemberModel;
use app\model\Post as PostModel;
use app\service\Member as MemberService;
use support\ErrorCode;
use think\Exception;
use think\facade\Db;

class BookType
{

    public $memberService;

    public function __construct()
    {
        $this->memberService = new MemberService();
    }

    /**
     * 创建知识库
     * @param $data
     * @return BookTypeModel|\think\Model
     * @throws Exception
     */
    public function create($data)
    {
        $createdBy = $this->memberService->getTenantInfo($data['tenant_id'], 'id,created_by');
        if ($createdBy['data']['created_by'] != $data['created_by']) {
            // 不是超级管理员,无法创建知识库
            throw new Exception('not is super user', ErrorCode::CREATE_KNOWLEDGE_BOOK_NOT_IS_SUPER_USER);
        }
        return BookTypeModel::create($data);
    }


    /**
     * 更新知识库
     * @param $data
     * @return BookTypeModel
     * @throws Exception
     */
    public function update($data)
    {
        $bookTypeModel = BookTypeModel::where(['id' => $data['id']])->find();
        if (empty($bookTypeModel)) {
            throw new Exception('book is not exist', ErrorCode::UPDATE_KNOWLEDGE_IS_NOT_EXIST);
        }
        $createdBy = $this->memberService->getTenantInfo($data['tenant_id'], 'id,created_by');
        if ($createdBy['data']['created_by'] != $data['created_by']) {
            // 不是超级管理员,无法编辑知识库
            throw new Exception('not is super user', ErrorCode::UPDATE_KNOWLEDGE_BOOK_NOT_IS_SUPER_USER);
        }
        return BookTypeModel::update($data);
    }

    /**
     * 删除知识库
     * @param $data
     * @return bool
     * @throws Exception
     * @throws \Throwable
     */
    public function delete($data): bool
    {
        Db::startTrans();
        try {
            $bookTypeModel = BookTypeModel::where(['id' => (int)$data['id']])->find();
            if (empty($bookTypeModel)) {
                throw new Exception('book id is not exist', ErrorCode::DELETE_KNOWLEDGE_IS_NOT_EXIST);
            }
            // 删除所有知识库权限
            if (!empty($data['id'])) {
                MemberModel::where(['type' => 'knowledge', 'type_id' => $data['id'], 'tenant_id' => $data['tenant_id']])->delete();
            }
            // 删除所有知识本权限
            $deleteBookId = PostModel::where(['book_type_id' => $data['id']])->column('id');
            if (!empty($deleteBookId)) {
                MemberModel::where(['type' => 'book', 'type_id' => $deleteBookId, 'tenant_id' => $data['tenant_id']])->delete();
            }
            // 删除当前知识本下的所有文件
            PostModel::where(['book_type_id' => $data['id'], 'tenant_id' => $data['tenant_id']])->delete();
            // 删除知识库
            $res = BookTypeModel::where(['id' => (int)$data['id']])->delete();
            Db::commit();
            return $res;
        } catch (\Throwable $e) {
            Db::rollback();
            throw $e;
        }
    }

    /**
     * 更新时间
     * @param $id
     * @return string
     */
    public function updateTime($id): string
    {
        if (!empty($id)) {
            $updData['id'] = $id;
            $updData['updated'] = time();
            return BookTypeModel::update($updData);
        }
        return '';
    }
}